/********************************************************************************
 *   Copyright 2007 SIP Response
 *   Copyright 2007 Michael D. Cohen
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 ********************************************************************************/
package com.sipresponse.mp3wavemachine.example;

import java.awt.BorderLayout;
import javax.media.MediaLocator;
import javax.media.format.AudioFormat;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JButton;

import com.sipresponse.mp3wavemachine.FormatConverter;
import com.sipresponse.mp3wavemachine.IConverterListener;

import java.awt.Rectangle;
import java.awt.Dimension;
import javax.swing.JTextArea;
import java.awt.Color;

/**
 * Test application for the FormatConverter
 *
 * @author Michael Cohen
 */
public class ConverterApplication implements IConverterListener
{
    // ************************************************************************
    //  Attributes
    // ************************************************************************
    private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,17"
    private JPanel jContentPane = null;
    private JMenuBar jJMenuBar = null;
    private JButton testButton = null;
    private static ConverterApplication application;
    private JTextArea jTextArea = null;
    private JLabel result = null;
    /**
     * This method initializes jTextArea	
     * 	
     * @return javax.swing.JTextArea	
     */
    private JTextArea getJTextArea()
    {
        if (jTextArea == null)
        {
            jTextArea = new JTextArea();
            jTextArea.setText("This test application will convert a wave file (\"C:\\test.wav\") to an MP3 file (\"C:\\test.mp3\")");
            jTextArea.setWrapStyleWord(true);
            jTextArea.setEditable(false);
            jTextArea.setLineWrap(true);
            jTextArea.setBounds(new Rectangle(17, 9, 264, 44));
        }
        return jTextArea;
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                application = new ConverterApplication();
                application.getJFrame().setVisible(true);
            }
        });
    }

    // ************************************************************************
    //  Instance Methods
    // ************************************************************************
    /**
     * This method initializes testButton   
     *  
     * @return javax.swing.JButton  
     */
    private JButton getTestButton()
    {
        if (testButton == null)
        {
            testButton = new JButton();
            testButton.setText("Test!");
            testButton.setBounds(new Rectangle(76, 78, 135, 40));
            testButton.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent e)
                {
                    FormatConverter converter = new FormatConverter(application);
                    result.setText("Converting...");
                    converter.convert(new MediaLocator("file://C:/test.wav"), 
                                      new MediaLocator("file://C:/test.mp3"),
                                      false);
                }
            });
        }
        return testButton;
    }

    /**
     * This method initializes jFrame
     * 
     * @return javax.swing.JFrame
     */
    private JFrame getJFrame()
    {
        if (jFrame == null)
        {
            jFrame = new JFrame();
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setJMenuBar(getJJMenuBar());
            jFrame.setSize(303, 214);
            jFrame.setContentPane(getJContentPane());
            jFrame.setTitle("Application");
        }
        return jFrame;
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane()
    {
        if (jContentPane == null)
        {
            result = new JLabel();
            result.setBounds(new Rectangle(24, 134, 250, 31));
            result.setBackground(Color.white);
            result.setHorizontalAlignment(SwingConstants.CENTER);
            result.setText("");
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getTestButton(), null);
            jContentPane.add(getJTextArea(), null);
            jContentPane.add(result, null);
        }
        return jContentPane;
    }

    /**
     * This method initializes jJMenuBar	
     * 	
     * @return javax.swing.JMenuBar	
     */
    private JMenuBar getJJMenuBar()
    {
        if (jJMenuBar == null)
        {
            jJMenuBar = new JMenuBar();
        }
        return jJMenuBar;
    }

    // ************************************************************************
    //  Interface Definition
    // ************************************************************************

    // ************************************************************************
    //  Overrides / Interface Implementation
    // ************************************************************************
    public void onConverterEvent(int status, int reason)
    {
        if (IConverterListener.CONVERTER_SUCCESS == status)
        {
            result.setText("File converted.");
        }
        else
        {
            result.setText("Conversion failed:  " + status + ", " + reason);
        }
    }
    
    // ************************************************************************
    //  Nested / Inner Classes
    // ************************************************************************

}
